<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input
        @keyup.enter.native="handleFilter"
        style="width: 200px"
        class="filter-item"
        placeholder="消息标题"
        v-model="listQuery.title"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        @click="handleCreate"
        type="primary"
        icon="edit"
      >
        添加
      </el-button>
    </div>
    <el-table
      :data="listObj"
      v-loading.body="listLoading"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="序号" type="index" width="65" />
      <el-table-column align="center" label="消息标题">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加用户姓名">
        <template slot-scope="scope">
          <span>{{ scope.row.createUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="listQuery.page"
        :page-sizes="[20, 50, 100, 150]"
        :page-size="listQuery.limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      width="35%"
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
       ref="formTempRef"
        :rules="rules"
        :model="formTemp"
        label-width="100px"
      >
        <el-form-item label="消息标题" prop="title">
          <el-input v-model="formTemp.title" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input
            type="textarea"
            :rows="4"
            v-model="formTemp.content"
            placeholder="请输入消息内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()"> 取 消 </el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="create()"
        >
          确 定
        </el-button>
        <el-button v-else type="primary" @click="update()"> 确 定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSysMessage,
  delSysMessage,
  getSysMessage,
  sysMessageList,
  putSysMessage
} from '@/api/system/sys/message'

export default {
  name: 'Message',
  data() {
    return {
      formTemp: {
        title: undefined,
        content: undefined
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入消息内容',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 128,
            message: '长度在xx以内个字符',
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: '请输入消息内容',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 1024,
            message: '长度在xx以内个字符',
            trigger: 'blur'
          }
        ]
      },
      // 列表对象集
      listObj: null,
      // 列表总数
      total: null,
      // 等待加载
      listLoading: true,
      // 列表查询条件
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      // 是否弹出（添加|更新）
      dialogFormVisible: false,
      // update|create
      dialogStatus: '',
      textMap: {
        // 表单title
        update: '更新',
        create: '添加'
      }
    }
  },
  // 初始化
  created() {
    this.getList()
  },
  methods: {
    // 实体列表数据
    getList() {
      this.listLoading = true
      sysMessageList(this.listQuery).then((response) => {
        this.listObj = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 列表查询
    handleFilter() {
      this.getList()
    },
    // pageSize(页码)改变时会触发
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    // currentPage(当前页) 改变时会触发
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    // 打开添加表单弹窗
    handleCreate() {
      this.resetFormTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 获取详情，并打开更新表单弹窗
    handleUpdate(row) {
      getSysMessage(row.id).then((response) => {
        this.formTemp = response.data
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
      })
    },
    // 打开删除确认弹窗
    handleDelete(row) {
      this.$confirm('此操作将数据删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delSysMessage(row.id).then((res) => {
          if (res && res.status === 200) {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          }
        })
      })
    },
    // 添加新实体
    create() {
      addSysMessage(this.formTemp).then((res) => {
        if (res && res.status === 200) {
          this.dialogFormVisible = false
          this.getList()
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },
    // 取消编辑表单
    cancel(formName) {
      this.dialogFormVisible = false
    },
    // 更新实体
    update(formName) {
      this.$confirm('此操作将数据更新, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dialogFormVisible = false
        putSysMessage(this.formTemp.id, this.formTemp).then((res) => {
          if (res && res.status === 200) {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: '更新',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          }
        })
      })
    },
    // 初始化表单字段
    resetFormTemp() {
      this.formTemp = {}
      this.formTemp.id = undefined
      this.formTemp.title = undefined
      this.formTemp.content = undefined
    }
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 15px;
}
</style>
